﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKB.Common;
using HKB.Common.HKB.Common;
using HKB.Data;
using HKB.Data.Bll;
using HKB.Web;
using HKB.Web.Shop.Member;
public partial class Member_Shop_NewsAdd : Member
{
	protected void Page_Load(object sender, EventArgs e)
	{
        if (!IsPostBack)
        {
            if (Request.QueryString["newsID"] != null || Request.QueryString["newsID"] != "")
            {
                HdnewsID.Value = Request.QueryString["newsID"];
                visibaleControl();
                
            }else
                base.Response.Write("<script>alert('主键ID丢失');</script>");
            
        }
	}

    private void visibaleControl()
    {
        if (string.IsNullOrEmpty(HdnewsID.Value))
        {
            ibtn.Visible = true;
            ibtnUpdate.Visible = false;
        }
        else
        {
            ibtn.Visible = false;
            ibtnUpdate.Visible = true;
            string text = "SELECT [newsID],[newsType],[newsTitle],[newsContext],[newsSource],[Createtime],[Updatetime],[ShopId],[UserId] FROM [DB_HKB].[dbo].[HLShop_News] WHERE newsID = '" + HdnewsID.Value + "' ";
            DataTable dataTable = this.conn.GetDataTable(text);
            if (dataTable.Rows.Count > 0)
            {
                txtTitle.Text = dataTable.Rows[0]["newsTitle"].ToString();
                txtSource.Text = dataTable.Rows[0]["newsSource"].ToString();
                txtContext.Text = dataTable.Rows[0]["newsContext"].ToString();
                ddlType.SelectedItem.Text = dataTable.Rows[0]["newsType"].ToString();
            }
        }
    }

	protected void ibtn_Click(object sender, ImageClickEventArgs e)
	{
		int UserID = base.user.UserID;
		int ShopID = base.user.ShopID;
		string strTitle = this.txtTitle.Text;
		string strContext = this.txtContext.Text;
		string strSource = this.txtSource.Text;
		string strType = this.ddlType.SelectedItem.Text;
        try
        {
            IDbDataParameter[] myparam_infr = new IDbDataParameter[]
		{
			this.conn.CreateParameter("@newsType", DbType.String, strType),
			this.conn.CreateParameter("@newsTitle", DbType.String, strTitle),
			this.conn.CreateParameter("@newsContext", DbType.String, strContext),
			this.conn.CreateParameter("@newsSource", DbType.String, strSource),
			this.conn.CreateParameter("@ShopId", DbType.String, ShopID),
			this.conn.CreateParameter("@UserId", DbType.String, UserID),
		};
            string text = "INSERT INTO [HLShop_News] (";
            text += "[newsType], [newsTitle], [newsContext], [newsSource], [Createtime], [Updatetime], [ShopId], [UserId]";
            text += ") VALUES (";
            object obj = text;
            text = string.Concat(new object[]
			{
				obj,
				"@newsType, @newsTitle, @newsContext, @newsSource, getdate(), getdate(), @ShopId, @UserId"
			});
            text += ")";
            this.conn.Execute(text, myparam_infr);
            this.txtTitle.Text = string.Empty;
            base.Response.Write("<script>alert('保存成功');</script>");

        }
        catch (Exception ex)
        {
            base.Response.Write("<script>alert('出错!保存失败!');</script>");
        }
	}

    protected void ibtnUpdate_Click(object sender, ImageClickEventArgs e)
    {
        int UserID = base.user.UserID;
        int ShopID = base.user.ShopID;
        string strTitle = this.txtTitle.Text;
        string strContext = this.txtContext.Text;
        string strSource = this.txtSource.Text;
        string strType = this.ddlType.SelectedItem.Text;
        try
        {
            IDbDataParameter[] myparam_infr = new IDbDataParameter[]
		{
			this.conn.CreateParameter("@newsType", DbType.String, strType),
			this.conn.CreateParameter("@newsTitle", DbType.String, strTitle),
			this.conn.CreateParameter("@newsContex", DbType.String, strContext),
			this.conn.CreateParameter("@newsSource", DbType.String, strSource),
		};

            string text = "UPDATE [HLShop_News] SET ";
            text += "[newsType]=@newsType, [newsTitle]=@newsTitle, [newsContext]=@newsContex, [newsSource]=@newsSource, [Updatetime]=getdate() ";
            text += " WHERE newsID = '" + HdnewsID.Value + "'";
            this.conn.Execute(text, myparam_infr);
            base.Response.Write("<script>alert('保存成功');</script>");

        }
        catch (Exception ex)
        {
            base.Response.Write("<script>alert('出错!保存失败!');</script>");
        }
    }
}